package com.lzy.crm.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.util.BootStrapReturn;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.domain.Profit;
import com.lzy.crm.domain.ProfitRule;
import com.lzy.crm.service.ProfitRuleService;
import com.lzy.crm.service.ProfitService;
import com.lzy.crm.util.PpUserConstants;
import com.lzy.crm.vo.ProfitVo;



/**
 * 分润控制器
 * @author csp
 *
 */
@Controller
@RequestMapping("/admin")
public class ProfitController {
	
	Logger logger= Logger.getLogger(ProfitController.class);
	
	@Resource
	ProfitService profitService;
	
	
	@Resource
	ProfitRuleService profitRuleService;
	
	
	@RequestMapping("/profit_mobile")  
	public String system(HttpServletRequest request,ModelMap model) {
		
		return "mobile/profit/profit_list";
	}
	
	@RequestMapping(path="/profit_data",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String profit_data(HttpServletRequest request, @RequestParam Map<String, String> form,
			ModelMap model) {
		BootStrapReturn<ProfitVo> result = new BootStrapReturn<ProfitVo>();
		try {
			PpUser user = (PpUser)request.getSession().getAttribute("userSession");
			Integer currentPage = Integer.parseInt(request.getParameter("numPerPage"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageNum"));
			String uid = request.getParameter("gameUserId");
			PageQuery pageQuery = new PageQuery(numPerPage,currentPage);
			pageQuery.setIsQueryTotal(true); 
			List<ProfitVo>  profitList = profitService.getProfit(user.getUid(),uid, pageQuery);
			result.setPage(pageQuery.getPageIndex());
			result.setTotal(pageQuery.getTotalCount());
			result.setRows(profitList);
		} catch (Exception e) {
			logger.error("分润列表", e);
		}
		return JsonUtil.toJson(result) ;
	}
	
	
	/**
	 *  '分润规则表'
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/profitRule_mobile")  
	public String profit(HttpServletRequest request,ModelMap model) {
		PpUser user = (PpUser)request.getSession().getAttribute("userSession");
		String prifitLevel = user.getUserLevel()!=null?user.getUserLevel().toString():null;
		if(user.getRid().compareTo( PpUserConstants.USER_TYPE_MANAGE)==-1){
			prifitLevel=null;
		}
		List<ProfitRule> profitRuleList = profitRuleService.getProfitRuleList(null, prifitLevel);
		model.put("profitRuleList", profitRuleList);
		return "mobile/profit/profitRule_list";
	}
	
	
	
	

}
